package com.ceres.system.dao;

import com.ceres.system.pojo.Customer;
import com.ceres.system.pojo.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;

@Component
public interface UserMapper extends Mapper<User> {
    /**
     * 插入平台用户id和角色id到关联表
     * @param roleId
     * @param userId
     */
    @Insert("insert into t_cust_user_role values(#{cuid},#{rid})")
    void addUserAndRole(@Param("rid") Integer roleId, @Param("cuid") Integer userId);

    /**
     * 根据用户id查询关联角色
     * @param id
     * @return
     */
    @Select("select rid from t_cust_user_role where cuid=#{id}")
    Integer[] findRoleByUser(Integer id);

    /**
     * 根据用户id查询关联角色
     * @param id
     */
    @Delete("delete from t_cust_user_role where cuid=#{id}")
    void deleteRolesByUserId(Integer id);

    /**
     * 更具登录名查询用户
     * @param username
     * @return
     */
    @Select("select * from t_cust_user where username=#{username}")
    User findUserByUserName(String username);

    /**
     * 根据农业组织id查询农业组织
     * @param customerId
     * @return
     */
    @Select("select * from t_customer where id=#{customerId}")
    Customer findCustomerById(Integer customerId);

//    @Select("select * from t_cust_user where name=#{name}")
//    @Select("<script>select * from t_cust_user where <if test=\"queryCondition!=null and queryCondition!=''\"> name=#{queryCondition} or username=#{queryCondition} </if> and customerId=#{customerId} </script>")
//    select * from t_cust_user where <if>(customer_id=1 and name="甘恢弘" OR username="derder")</if>
//    @Select("<script>select * from t_cust_user where customerId=#{customerId} <if test=\"queryCondition!=null and queryCondition!=''\">adn name=#{queryCondition} OR username=#{queryCondition}</if></script>")
    @Select("<script>select * from t_cust_user <where> <if test=\"queryCondition!=null and queryCondition!=''\"> name=#{queryCondition} or username=#{queryCondition} </if> and customer_id=#{customer_id}</where> </script>")
    Page<User> findByCondition(@Param("queryCondition") String queryCondition,@Param("customer_id") Integer customerId);

    @Update("update t_cust_user_info set theme=#{theme} where cust_user_id=#{userId}")
    void changeTheme(@Param("theme") String theme,@Param("userId") Integer userId);

    @Update("update t_cust_user_info set logo=#{logo} where cust_user_id=#{userId}")
    void updateLogo(@Param("username")String username,@Param("logo")String logo);

    @Update("update t_cust_user_info set updateProjectName=#{updateProjectName} where cust_user_id=#{userId}")
    void updateProjectName(@Param("username")String username,@Param("updateProjectName")String updateProjectName);
}
